package com.jiawa.wiki.service;

import com.jiawa.wiki.domain.Ebook;
import com.jiawa.wiki.domain.EbookExample;
import com.jiawa.wiki.mapper.EbookMapper;
import com.jiawa.wiki.req.EbookReq;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import resp.EbookResp;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EbookService {

    @Resource
    public EbookMapper ebookMapper;


    public List<EbookResp> getlist(EbookReq req){

        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();

        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%"+req.getName()+"%");
        }


        //处理响应数据
        //存完整数据的列表[[],[],[],[id=xniks],[]]

        List<Ebook> ebooklist=ebookMapper.selectByExample(ebookExample);

        List<EbookResp> respList = new ArrayList<>();

        //循环五次


        //空列表
        //return ebookMapper.selectByExample();

        for(Ebook i:ebooklist){

            EbookResp ebookResp = new EbookResp();
            BeanUtils.copyProperties(i,ebookResp);
            respList.add(ebookResp);


        }

        return respList;
    }

}
